package cc.toprank.syncdatatask.config;

import cc.toprank.syncdatatask.listener.SubscribeListener1;
import cc.toprank.syncdatatask.util.GlobalContants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description 订阅消息队列
 * User: WeiDa
 * Date: 2017-5-7
 * Time: 17:00
 */
@Configuration
public class PublishSubscribeConfig {
	@Autowired(required = false)
	RabbitConfig rabbitconfig;

 	@Bean
    public Queue myQueue1() {
       Queue queue=new Queue("queue"+ GlobalContants.ARCHIVE_ID);
       return queue;
    }

/*	@Bean
	public Queue myQueue2() {
		Queue queue=new Queue("delayQueue"+ GlobalContants.ARCHIVE_ID);
		return queue;
	}*/
 	
 	@Bean
 	public FanoutExchange fanoutExchange(){
 		FanoutExchange fanoutExchange=new FanoutExchange("fanout");
 		return fanoutExchange;
 	}
 	
 	@Bean
 	public Binding binding1(){
 		Binding binding= BindingBuilder.bind(myQueue1()).to(fanoutExchange());
 		return binding;
 	}

 	
 	 //订阅者监听器1 监听实时消息
    @Bean
    public SubscribeListener1 subscribeListener1() {
    	return new SubscribeListener1();
    }
    
    @Bean
    public MessageListenerAdapter mailListenerAdapter() {
	    MessageListenerAdapter adapter = new MessageListenerAdapter();
	    adapter.setDelegate(subscribeListener1());
	    adapter.setDefaultListenerMethod("subscribe");
	    return adapter;
    }
    
    @Bean
    public SimpleMessageListenerContainer messageListenerContainer() {
	    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	    container.setConnectionFactory(rabbitconfig.connectionFactory());
	    container.setMessageListener(mailListenerAdapter());
	    container.setQueues(myQueue1());
	    return container;
    }




/*	@Bean
	public FanoutExchange fanoutExchange2(){
		FanoutExchange fanoutExchange=new FanoutExchange("fanout2");
		return fanoutExchange;
	}

	@Bean
	public Binding binding2(){
		Binding binding= BindingBuilder.bind(myQueue2()).to(fanoutExchange2());
		return binding;
	}

	//订阅者监听器2 监听手动消息

	@Bean
	public MessageListenerAdapter delayListenerAdapter() {
		MessageListenerAdapter adapter = new MessageListenerAdapter();
		adapter.setDelegate(subscribeListener1());
		adapter.setDefaultListenerMethod("subscribe2");
		return adapter;
	}

	@Bean
	public SimpleMessageListenerContainer messageListenerContainer2() {
		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
		container.setConnectionFactory(rabbitconfig.connectionFactory());
		container.setMessageListener(delayListenerAdapter());
		container.setQueues(myQueue2());
		return container;
	}*/

}
